package com.myapp.ruyixing.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.text.format.DateFormat;
import android.text.format.DateUtils;

public class TimeUtils {

	public static final String DEFAULT_DATE_FORMATER = "yyyy年MM月dd日";
	public static final String EE_DATE_FORMATER = "yyyy-MM-dd EE";
	public static final String DEFAULT_TIME_FORMATER = "hh:mm";

	public static final CharSequence getEEDayString(Calendar c) {
		return DateFormat.format(EE_DATE_FORMATER, c);
	}

	public static final CharSequence getTimeString(Calendar c) {
		return DateFormat.format(DEFAULT_TIME_FORMATER, c);
	}

	public static final CharSequence getTodayString() {
		return DateFormat.format(DEFAULT_DATE_FORMATER, Calendar.getInstance());
	}

	public static final CharSequence getDayString(Calendar c) {
		return DateFormat.format(DEFAULT_DATE_FORMATER, c);
	}

	public static final CharSequence getDayString(Calendar c, int dayGap) {
		c.add(Calendar.DAY_OF_MONTH, dayGap);
		return DateFormat.format(DEFAULT_DATE_FORMATER, c);
	}

	public static final CharSequence getString(Calendar c, String formatter) {
		return DateFormat.format(formatter, c);
	}

	public static final Calendar parsrDate(String dateString) {
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_FORMATER);
		Calendar c = Calendar.getInstance();

		try {
			Date date = format.parse(dateString);
			c.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return c;
	}

	public static final Calendar parsrEEDate(String dateString) {
		SimpleDateFormat format = new SimpleDateFormat(EE_DATE_FORMATER);
		Calendar c = Calendar.getInstance();

		try {
			Date date = format.parse(dateString);
			c.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return c;
	}

	public static final Calendar parsrTime(String dateString) {
		SimpleDateFormat format = new SimpleDateFormat(DEFAULT_TIME_FORMATER);
		Calendar c = Calendar.getInstance();

		try {
			Date date = format.parse(dateString);
			c.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return c;
	}

	public static final Calendar getCalendar(int year, int monthOfYear,
			int dayOfMonth) {
		Calendar c = Calendar.getInstance();

		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, monthOfYear);
		c.set(Calendar.DAY_OF_MONTH, dayOfMonth);

		return c;
	}

	public static final Calendar getCalendar(int hourOfDay, int minute) {
		Calendar c = Calendar.getInstance();

		c.set(Calendar.HOUR_OF_DAY, hourOfDay);
		c.set(Calendar.MINUTE, minute);

		return c;
	}
	

	public static String getRelativeDateTimeString(Context c, long time) {

	    long now = System.currentTimeMillis();
	    long duration = Math.abs(now - time);
	    long minResolution = 0;
	    long transitionResolution = 3 * DateUtils.DAY_IN_MILLIS;
	    int flags = DateUtils.FORMAT_SHOW_DATE;
	    
	    // getRelativeTimeSpanString() doesn't correctly format relative dates
	    // above a week or exact dates below a day, so clamp
	    // transitionResolution as needed.
	    if (transitionResolution > DateUtils.WEEK_IN_MILLIS) {
	        transitionResolution = DateUtils.WEEK_IN_MILLIS;
	    } else if (transitionResolution < DateUtils.DAY_IN_MILLIS) {
	        transitionResolution = DateUtils.DAY_IN_MILLIS;
	    }
	
	    String result = null;
	    if (duration < transitionResolution) {
	        CharSequence relativeClause = DateUtils.getRelativeTimeSpanString(time, now, minResolution, flags);
	        
	        //将2天前转成前天
	        result = relativeClause.toString();
	        String twoDaysAgo = "2 天前";
			String beforeYesterday = "前天";
			
			if(result != null && result.contains(twoDaysAgo)){
				result = beforeYesterday;
			}
			
			return result;
	    } else {
	        CharSequence dateClause = DateUtils.getRelativeTimeSpanString(c, time, false);
	        
	        return dateClause.toString();
	    }
	}
}
